from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException

from ui_framework.page.get_app_datas import GetAppDatas


class BasePage:
    """
    将公共的部分放入base类中，在别的类中直接继承就可以了
    将公共部分封装成方法，再在别的类中引用即可
    """
    def __init__(self,driver:WebDriver):
        self.driver = driver

    def decorator(decorator_find):
        blacklist = ['//*[@resource-id="com.xueqiu.android:id/iv_close"]']
        def wrapper(*args,**kwargs):
            self = args[0]  # *args包含三个元素，取第一个
            try:
                return decorator_find(*args, **kwargs)
            except Exception:
                for ele_xpath in blacklist:
                    eles = self.finds(MobileBy.XPATH, ele_xpath)
                    if len(eles) > 0:
                        eles[0].click()
                        return decorator_find(*args, **kwargs)
        return wrapper

    @decorator
    def find(self,locator,value):
        return self.driver.find_element(locator,value)

    def finds(self,locator,value):
        return self.driver.find_elements(locator,value)

    def find_and_click(self,locator,value):
        return self.find(locator,value).click()

    def find_and_send(self,locator,value,contents):
        return self.find(locator,value).send_keys(contents)

    def swipe_down(self):
        size = self.driver.get_window_size()
        width = size.get('width')
        height = size.get('height')
        start_width = width*0.5
        start_height = height*0.25
        end_width = width*0.5
        end_height = height*0.75
        self.driver.swipe(start_width,start_height,end_width,end_height,1000)